/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AsignatureGui.java
 *
 * Created on 17/06/2011, 11:58:34 AM
 */

package GUI.ATTENDANT;

import Procedures.DBConection;
import entities.Asignature;

/**
 *
 * @author SIX-14
 */
public class AsignatureGui extends javax.swing.JPanel {

    /** Creates new form AsignatureGui */
    public AsignatureGui(DBConection myConection, int action, Asignature asignature) {
        initComponents();
        setMyConection(myConection);
        setAction(action);
        setAsignature(asignature);
        fillcombobox();
        initComponents2();

    }

    private DBConection myConection;
    private int action;
    private Asignature asignature;

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public Asignature getAsignature() {
        return asignature;
    }

    public void setAsignature(Asignature asignature) {
        this.asignature = asignature;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }


    public void fillcombobox(){
        getMyConection().sendSQLQuery("SELECT name FROM ESPECIALITY where active=1;");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox1.removeAllItems();
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }
    }

    public void initComponents2(){
        switch(getAction()){
            case 1: jButton1.setText("Agregar"); break;
            case 2: jButton1.setText("Modificar"); fillfields(); jCheckBox1.setEnabled(true); break;
            case 3: jButton1.setText("Eliminar"); fillfields(); 
                    jTextField1.setEditable(false); jTextField2.setEditable(false); jComboBox1.setEnabled(false); break;

        }

    }

    public void fillfields(){
        jTextField1.setText(getAsignature().getId());
        jTextField2.setText(getAsignature().getName());
        getMyConection().sendSQLQuery("SELECT name FROM ESPECIALITY WHERE  CODE='"+getAsignature().getEspeciality()+"';");
        jComboBox1.setSelectedItem(getMyConection().getLastResultSetColumnStrings(1)[0]);
        jCheckBox1.setSelected(getAsignature().isActive());
    }





    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Codigo de la materia:");

        jLabel2.setText("Especialidad a la que pertenece");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Nombre de la Materia:");

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Activa");
        jCheckBox1.setEnabled(false);

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel1)
                                .addComponent(jLabel3)
                                .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jButton1)))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(56, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(jButton1.getText().equals("Agregar")){
            getMyConection().sendSQL("INSERT INTO ASIGNATURE SET ID='"+jTextField1.getText()+"', NAME='"+jTextField2.getText()+"', ESPECIALITY=(SELECT CODE FROM ESPECIALITY WHERE NAME='"+jComboBox1.getSelectedItem().toString()+"'), ACTIVE=1;");
        }else if(jButton1.getText().equals("Modificar")){
            int active;
            if(jCheckBox1.isSelected()){
                active=1;
            }else{
                active=0;
            }
            getMyConection().sendSQL("UPDATE ASIGNATURE SET ID='"+jTextField1.getText()+"', NAME='"+jTextField2.getText()+"', ESPECIALITY=(SELECT CODE FROM ESPECIALITY WHERE NAME='"+jComboBox1.getSelectedItem().toString()+"'), ACTIVE="+active+";");

        }else if(jButton1.getText().equals("Eliminar")){
            getMyConection().sendSQL("UPDATE  ASIGNATURE SET ACTIVE=0  WHERE ID='"+jTextField1.getText()+"';");

        }
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
